package ferran.alcazar.one.datos;

import java.util.Set;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.ArrayAdapter;

public class BluetoothService extends BroadcastReceiver  {
	private BluetoothAdapter b;
	private ArrayAdapter<String> mArrayPaired;

	private ArrayAdapter<String> mArrayAdapter;


	public BluetoothService() {
		b = BluetoothAdapter.getDefaultAdapter();
		if(b == null) {
			Log.e("piedra papel tijera", "Bluetooth no esta habilitado para este telefono");
		}
	}
	
	public void discoverPairedDevices() {
		Set<BluetoothDevice> pairedDevices = b.getBondedDevices();
		// If there are paired devices
		if (pairedDevices.size() > 0) {
		    // Loop through paired devices
		    for (BluetoothDevice device : pairedDevices) {
		        // Add the name and address to an array adapter to show in a ListView
		    	mArrayPaired.add(device.getName() + "\n" + device.getAddress());
		    }
		}
	}
	
	public BluetoothAdapter getB() {
		return b;
	}


	public void setB(BluetoothAdapter b) {
		this.b = b;
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // Add the name and address to an array adapter to show in a ListView
            mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
        }
	}

	/**
	 * @return the mArrayAdapter
	 */
	public ArrayAdapter<String> getmArrayAdapter() {
		return mArrayAdapter;
	}

	/**
	 * @param mArrayAdapter the mArrayAdapter to set
	 */
	public void setmArrayAdapter(ArrayAdapter<String> mArrayAdapter) {
		this.mArrayAdapter = mArrayAdapter;
	}

	/**
	 * @return the mArrayPaired
	 */
	public ArrayAdapter<String> getmArrayPaired() {
		return mArrayPaired;
	}

	/**
	 * @param mArrayPaired the mArrayPaired to set
	 */
	public void setmArrayPaired(ArrayAdapter<String> mArrayPaired) {
		this.mArrayPaired = mArrayPaired;
	}
}
